<?php

class ApplicationFramework extends AFramework
{

	private 	$seo_data;
	private     $right_side_plugins;
	private 	$params;
	private		$core_controller;
	private		$plugin_controller;
	protected 	$plugin;
	protected 	$method;

	function __construct() {}
	function __destruct() {}

	public function load($guimode="full")
	{
		// Start out optimistic, and hope for the best
		$this->ok = true;

		// Load the Framework config file into memory
		$this->loadConfig(CONFIG);

		// Let the lower layers know what realm we are dealing with
		// useful for checking if logged in or different subscription levels
		$this->realm = 'front';

		// Initialize the View layer
		$this->view = new Savant3();
		$this->view()->addPath("template",APP_TEMPLATE_DIR);
		$this->view()->addPath("template",CORE_TEMPLATE_DIR);
		$this->view()->addPath("template",PLUGIN_DIR);
		$this->view()->setAutoload(false);

		// If you're doing ajax you'll want at least two modes: full and ajax
		$this->guimode = $guimode;
		$this->view()->assign("guimode",$this->guimode);

		$this->parseURL();
	}

	public function run()
	{
		AFramework::debug(__METHOD__);

		$this->loadController($this->controller);
	}
	
	public function setAction($action)
	{
		$this->loadController($action);
	}

	public function parseURL($in_path_info=null)
	{
		AFramework::debug(__METHOD__." -> mode: ".$this->getGuiMode());
		switch ($this->getGuiMode())
		{
			case 'plugin':
				$this->plugin = $_GET['plugin'];
				$this->controller = $_GET['controller'];
				$this->method = $_GET['method'];
				break;

			default:
				$in_path_info = ((is_null($in_path_info)) ? $_GET['chunk'] : $in_path_info);
				AFramework::debug(__METHOD__." -> path: ".$in_path_info);
				$split_chunk = trim($in_path_info,'/ ');

				$parts = explode("/", $split_chunk);
				if($parts[0] == "plugins")
				{
					$this->plugin_controller = true;
					$root = "";
				}
				else
				{
					$root = APP_CONTROLLER_DIR;
				}
				$params = array();
				while(count($parts))
				{
					if(file_exists($root . $split_chunk. ".php"))
					{
						$this->controller = $split_chunk;
						$this->core_controller = false;
						break;
					}
					else
					{
						$params[] = array_pop($parts);
						$split_chunk = implode("/", $parts);
					}
				}

				if(!count($parts))
				{
					$this->controller = "home";
				}

				$this->params = implode("/", array_reverse($params));
				break;
		}
	}

	public function getParams()
	{
		return $this->params;
	}

	public function getRequestedPlugin()
	{
		return $this->plugin;
	}

	public function getRequestedMethod()
	{
		return $this->method;
	}

	public function loadController($in_controller)
	{
		if($this->core_controller)
		{
			$this->view()->assign('template', str_replace("/controllers/", "/templates/", $in_controller . ".tpl"));
			require($in_controller . ".php");
		}
		else
		{
			if($this->plugin_controller)
			{
				require($in_controller . ".php");
			}
			else
			{
				$this->view()->assign('template', $in_controller . ".tpl");
				require(APP_CONTROLLER_DIR . $in_controller . ".php");
			}
		}
	}

	public function getPrimaryTemplate()
	{
		return 'gui/'.$this->guimode.'.tpl';;
	}

}

?>
